Imports Cdm.DataCore.Custom

Partial Class DashboardControls_FMSAFiles
    Inherits System.Web.UI.UserControl
    Implements IDashBoard

    Private titleLabel As String = ""
    Dim pageSizeValue As Integer = 0

    Public Property Title() As String Implements IDashBoard.Title
        Get
            If lblTitle IsNot Nothing Then
                Return lblTitle.Text
            Else
                Return titleLabel
            End If
        End Get
        Set(ByVal value As String)
            If lblTitle IsNot Nothing Then
                lblTitle.Text = value
            End If
            titleLabel = value
        End Set
    End Property


    Public Property SortDirection() As System.Web.UI.WebControls.SortDirection Implements IDashBoard.SortDirection
        Get
            If ViewState("SortDirection") Is Nothing Then
                ViewState("SortDirection") = SortDirection.Ascending
            End If
            Return CType(ViewState("SortDirection"), SortDirection)
        End Get
        Set(ByVal value As System.Web.UI.WebControls.SortDirection)
            ViewState("SortDirection") = value
        End Set
    End Property


    Public Property PageNumber() As Integer Implements IDashBoard.PageNumber
        Get
            Return Me.FmsaFilesGridView.PageIndex
        End Get
        Set(ByVal value As Integer)
            Me.FmsaFilesGridView.PageIndex = value
        End Set
    End Property

    Public Property PageSize() As Integer Implements IDashBoard.PageSize
        Get
            If Not (Me.FmsaFilesGridView Is Nothing) Then
                Return Me.FmsaFilesGridView.PageSize
            Else
                Return pageSizeValue
            End If
        End Get
        Set(ByVal value As Integer)
            If Not (Me.FmsaFilesGridView Is Nothing) Then
                Me.FmsaFilesGridView.PageSize = value
                Me.NumberOfRowsDropDownList.SelectedValue = value.ToString()
            End If
            pageSizeValue = value
        End Set
    End Property

    Public Property SortField() As String Implements IDashBoard.SortField
        Get
            If ViewState("SortField") Is Nothing Then
                ViewState("SortField") = ""
            End If
            Return ViewState("SortField")
        End Get
        Set(ByVal value As String)
            ViewState("SortField") = value
        End Set
    End Property

    Protected Sub Page_Init(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Init

        AddHandler Me.NumberOfRowsDropDownList.SelectedIndexChanged, AddressOf NumberOfRowsDropDownList_SelectedIndexChanged

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Handles Me.Load

        If Not String.IsNullOrEmpty(titleLabel) Then
            Me.lblTitle.Text = titleLabel
        End If
        If pageSizeValue > 0 Then
            Me.PageSize = pageSizeValue
        End If

        If Not Me.IsPostBack Then
            GridViewSettingController.SetGridViewValues(Me, 5, "DisplayLastNameFirst", WebControls.SortDirection.Ascending)
            Me.BindGrid()
        End If

    End Sub

    Protected Sub NumberOfRowsDropDownList_SelectedIndexChanged(ByVal sender As Object, _
                                                                ByVal e As System.EventArgs) _
                                                                Handles NumberOfRowsDropDownList.SelectedIndexChanged

        If Me.NumberOfRowsDropDownList.SelectedValue = "-1" Then
            Me.FmsaFilesGridView.AllowPaging = False
        Else
            Me.FmsaFilesGridView.AllowPaging = True
            Me.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
            'Me.FmsaFilesGridView.PageSize = Me.NumberOfRowsDropDownList.SelectedValue
            Me.FmsaFilesGridView.PageIndex = 0
        End If

        BindGrid()
    End Sub

    Public Sub BindGrid() Implements IDashBoard.BindGrid
        Dim userid As Integer = UserAuthorization.CurrentUser.UserId
        Dim isAscending As Boolean = (Me.SortDirection.Equals(SortDirection.Ascending))
        Me.FmsaFilesGridView.DataSource = ParticipantDisplay.ListByFMSAUserID(userid, Me.SortField, isAscending)
        Me.FmsaFilesGridView.DataBind()
        GridViewSettingController.SaveSetting(Me)
    End Sub


    Protected Sub FmsaFilesGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles FmsaFilesGridView.DataBound
        AccessibilityController.AddImageToSortableGridHeaders(FmsaFilesGridView)
    End Sub

    Protected Sub grvSupportBrokerCases_PageIndexChanging(ByVal sender As Object, _
                                                          ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) _
                                                          Handles FMSAFilesGridView.PageIndexChanging

        Me.PageNumber = e.NewPageIndex
        BindGrid()

    End Sub

    Public Sub LoadControl1(ByVal sender As Object, _
                            ByVal e As System.EventArgs) _
                            Implements IDashBoard.LoadControl

        Me.Page_Load(sender, e)

    End Sub

    Protected Sub FmsaFilesGridView_Sorting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewSortEventArgs) Handles FmsaFilesGridView.Sorting
        If e.SortExpression.Equals(Me.SortField) Then
            ToggleSortDirection()
        Else
            Me.SortDirection = WebControls.SortDirection.Ascending
        End If
        Me.SortField = e.SortExpression

        BindGrid()
    End Sub

    Private Sub ToggleSortDirection()
        If Me.SortDirection.Equals(SortDirection.Ascending) Then
            SortDirection = WebControls.SortDirection.Descending
        Else
            SortDirection = WebControls.SortDirection.Ascending
        End If
    End Sub
End Class
